package com.xl.basic.controller;

import com.xl.basic.annotation.RoleIgnored;
import com.xl.basic.annotation.RoleValidated;
import com.xl.basic.entity.Result;
import com.xl.basic.entity.SPage;
import com.xl.basic.entity.UserInfo;
import com.xl.basic.entity.param.AddRoleParam;
import com.xl.basic.entity.param.QueryRoleParam;
import com.xl.basic.entity.param.UpdRoleParam;
import com.xl.basic.service.RoleService;
import com.xl.basic.utils.LoginUtils;
import com.xl.dao.domain.XlRole;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.constraints.NotNull;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author 钟勇
 * Create in 2023/8/31 9:44
 */
@Tag(name = "角色管理")
@RestController
@RequestMapping("/api/Role")
@RequiredArgsConstructor
@RoleValidated
public class RoleController {
    private final RoleService roleService;


    @Operation(summary = "新增角色")
    @PostMapping(value = "/addRole")
    Result<Object> addRole(@Validated AddRoleParam param) {
        Result<UserInfo> userInfoResult = LoginUtils.getUserInfo();
        return roleService.addRole(param, userInfoResult.getResult());
    }


    @Operation(summary = "编辑角色")
    @PostMapping(value = "/updRole")
    Result<Object> updRole(@Validated UpdRoleParam param) {
        Result<UserInfo> userInfoResult = LoginUtils.getUserInfo();
        return roleService.updRole(param, userInfoResult.getResult());
    }


    @Operation(summary = "删除角色")
    @PostMapping(value = "/delRole")
    Result<Object> delRole(@Validated @NotNull(message = "角色ID不能为空") Integer roleId) {
        Result<UserInfo> userInfoResult = LoginUtils.getUserInfo();
        return roleService.delRole(roleId, userInfoResult.getResult());
    }


    @Operation(summary = "查询角色列表")
    @PostMapping(value = "/queryRoleList")
    Result<List<XlRole>> queryRoleList(QueryRoleParam param) {
        return roleService.queryRoleList(param);
    }

    @Operation(summary = "查询角色分页")
    @PostMapping(value = "/queryRolePage")
    Result<List<XlRole>> queryRolePage(QueryRoleParam param, SPage sPage) {
        return roleService.queryRolePage(param, sPage);
    }


    @RoleIgnored
    @Operation(summary = "查询当前用户角色列表")
    @PostMapping(value = "/queryCurrUserRoleList")
    Result<List<XlRole>> queryCurrUserRoleList() {
        Result<UserInfo> userInfoRes = LoginUtils.getUserInfo();
        if (userInfoRes.isErr()) {
            return userInfoRes.create();
        }

        String userId = userInfoRes.getResult().getUserId();
        return queryRoleList(new QueryRoleParam().setUserId(userId));
    }

}
